﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PD.Web.DynamicData.Content
{
    /// <summary>
    /// Provides paging functionality for grid view control.
    /// </summary>
    public partial class GridViewPager : System.Web.UI.UserControl
    {
        /// <summary>
        /// The grid view control.
        /// </summary>
        private GridView gridView;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            Control c = Parent;
            while (c != null)
            {
                if (c is GridView)
                {
                    this.gridView = (GridView)c;
                    break;
                }

                c = c.Parent;
            }
        }

        /// <summary>
        /// Handles the TextChanged event of the TextBoxPage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void TextBoxPage_TextChanged(object sender, EventArgs e)
        {
            if (this.gridView == null)
            {
                return;
            }

            int page;
            if (int.TryParse(this.TextBoxPage.Text.Trim(), out page))
            {
                if (page <= 0)
                {
                    page = 1;
                }

                if (page > this.gridView.PageCount)
                {
                    page = this.gridView.PageCount;
                }

                this.gridView.PageIndex = page - 1;
            }

            this.TextBoxPage.Text = (this.gridView.PageIndex + 1).ToString(CultureInfo.CurrentCulture);
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the DropDownListPageSize control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.gridView == null)
            {
                return;
            }

            DropDownList dropdownlistpagersize = (DropDownList)sender;
            this.gridView.PageSize = Convert.ToInt32(dropdownlistpagersize.SelectedValue, CultureInfo.CurrentCulture);
            int pageindex = this.gridView.PageIndex;
            this.gridView.DataBind();
            if (this.gridView.PageIndex != pageindex)
            {
                // if page index changed it means the previous page was not valid and was adjusted. Rebind to fill control with adjusted page
                this.gridView.DataBind();
            }
        }

        /// <summary>
        /// Handles the PreRender event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (this.gridView != null)
            {
                this.LabelNumberOfPages.Text = this.gridView.PageCount.ToString(CultureInfo.CurrentCulture);
                this.TextBoxPage.Text = (this.gridView.PageIndex + 1).ToString(CultureInfo.CurrentCulture);
                this.DropDownListPageSize.SelectedValue = this.gridView.PageSize.ToString(CultureInfo.CurrentCulture);
            }
        }
    }
}
